package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.Department;
import sjtu.ist.warehouse4ws.model.PrivilegePojo;
import sjtu.ist.warehouse4ws.model.ShiroPermission;
import sjtu.ist.warehouse4ws.model.ShiroRole;
import sjtu.ist.warehouse4ws.model.ShiroUser;
import sjtu.ist.warehouse4ws.service.ShiroPermissionService;
import sjtu.ist.warehouse4ws.service.ShiroRoleService;
import sjtu.ist.warehouse4ws.service.ShiroUserService;

import com.opensymphony.xwork2.ActionSupport;

public class PrivilegeManage extends ActionSupport{
private ShiroUserService shiroUserService ;
private ShiroRoleService shiroRoleService;
private ShiroPermissionService shiroPermissionService;
private ArrayList<PrivilegePojo> privilegeInfoList=new ArrayList<PrivilegePojo>();	

private String editop;
private String deleteop;
private String wadlop;
private String wsdlop;
private String checkoutop;
private String checkin;
private int id;






public void setShiroUserService(ShiroUserService shiroUserService) {
	this.shiroUserService = shiroUserService;
}

public void setShiroRoleService(ShiroRoleService shiroRoleService) {
	this.shiroRoleService = shiroRoleService;
}



public void setShiroPermissionService(
		ShiroPermissionService shiroPermissionService) {
	this.shiroPermissionService = shiroPermissionService;
}

public String showPrivilegeInfo(){
	Subject currentUser = SecurityUtils.getSubject();
	Integer companyId= (Integer) currentUser.getSession().getAttribute("companyId");
	String  username=currentUser.getSession().getAttribute("username").toString();
	List<ShiroUser> userlist=shiroUserService.findCompanyUser(companyId);
	List<ShiroUser> userlist1=new ArrayList<ShiroUser>();
	for(ShiroUser user:userlist){
		if(!user.getUsername().toString().equals(username)){
			userlist1.add(user);
		}
	}
	for(ShiroUser user:userlist1){
		
		PrivilegePojo privilegeInfo=new PrivilegePojo();
		privilegeInfo.setUsername(user.getUsername());
		privilegeInfo.setUserId(user.getUserId());
		for(ShiroRole role:user.getShiroRoles()){
			privilegeInfo.setRole(role.getRolename());
			for(ShiroPermission p:role.getShiroPermissions()){
				if(p.getPermission().toString().equals("model:edit")){privilegeInfo.setModelEdit("1");}
				if(p.getPermission().toString().equals("model:editnull")){privilegeInfo.setModelEdit("0");}
				if(p.getPermission().toString().equals("model:delete")){privilegeInfo.setModelDelete("1");}
				if(p.getPermission().toString().equals("model:deletenull")){privilegeInfo.setModelDelete("0");}
				if(p.getPermission().toString().equals("model:checkin")){privilegeInfo.setModelCheckIn("1");}
				if(p.getPermission().toString().equals("model:checkinnull")){privilegeInfo.setModelCheckIn("0");}
				if(p.getPermission().toString().equals("model:checkout")){privilegeInfo.setModelCheckOut("1");}
				if(p.getPermission().toString().equals("model:checkoutnull")){privilegeInfo.setModelCheckOut("0");}
				if(p.getPermission().toString().equals("model:towsdl")){privilegeInfo.setModelToWsdl("1");}
				if(p.getPermission().toString().equals("model:towsdlnull")){privilegeInfo.setModelToWsdl("0");}
				if(p.getPermission().toString().equals("model:towadl")){privilegeInfo.setModelToWadl("1");}
				if(p.getPermission().toString().equals("model:towadlnull")){privilegeInfo.setModelToWadl("0");}
				
				if(p.getPermission().toString().equals("model:*")){
					privilegeInfo.setModelEdit("1");
					privilegeInfo.setModelDelete("1");
					privilegeInfo.setModelCheckIn("1");
					privilegeInfo.setModelCheckOut("1");
					privilegeInfo.setModelToWsdl("1");
					privilegeInfo.setModelToWadl("1");
				}
			}
		}
		privilegeInfoList.add(privilegeInfo);
		
		
	}
	return SUCCESS;
}	
public String  updatePrivilege(){
	System.out.print(id);
	ShiroUser user=shiroUserService.getUser(id);
	for(ShiroRole role:user.getShiroRoles()){
		if(editop.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:editnull")){
			shiroPermission.setPermission("model:edit");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(editop.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:edit")){	
				shiroPermission.setPermission("model:editnull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}
	
		if(deleteop.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:deletenull")){
			shiroPermission.setPermission("model:delete");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(deleteop.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:delete")){	
				shiroPermission.setPermission("model:deletenull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}
		
	
		if(wadlop.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:towadlnull")){
			shiroPermission.setPermission("model:towadl");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(wadlop.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:towadl")){	
				shiroPermission.setPermission("model:towadlnull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}	
		
	
		if(wsdlop.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:towsdlnull")){
			shiroPermission.setPermission("model:towsdl");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(wsdlop.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:towsdl")){	
				shiroPermission.setPermission("model:towsdlnull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}
	
		
		if(checkoutop.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:checkoutnull")){
			shiroPermission.setPermission("model:checkout");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(checkoutop.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:checkout")){	
				shiroPermission.setPermission("model:checkoutnull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}
	
		if(checkin.toString().equals("1")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:checkinnull")){
			shiroPermission.setPermission("model:checkin");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
			}
		}if(checkin.toString().equals("0")){
			for(ShiroPermission shiroPermission:role.getShiroPermissions()){
			if(shiroPermission.getPermission().toString().equals("model:checkin")){	
				shiroPermission.setPermission("model:checkinnull");
			shiroPermissionService.updateShiroPermission(shiroPermission);
			}
		}	
	}
		
	}
	return SUCCESS;
}

@JSON(name="privilegeInfosize")
public int getTotalprivilegeInfosize() {
	return this.privilegeInfoList.size();
}

@JSON(name ="privilegeInfo")
public List<PrivilegePojo> getCompanyList() {
	return this.privilegeInfoList;
}

public String getEditop() {
	return editop;
}

public void setEditop(String editop) {
	this.editop = editop;
}

public String getDeleteop() {
	return deleteop;
}

public void setDeleteop(String deleteop) {
	this.deleteop = deleteop;
}

public String getWadlop() {
	return wadlop;
}

public void setWadlop(String wadlop) {
	this.wadlop = wadlop;
}

public String getWsdlop() {
	return wsdlop;
}

public void setWsdlop(String wsdlop) {
	this.wsdlop = wsdlop;
}

public String getCheckoutop() {
	return checkoutop;
}

public void setCheckoutop(String checkoutop) {
	this.checkoutop = checkoutop;
}

public String getCheckin() {
	return checkin;
}

public void setCheckin(String checkin) {
	this.checkin = checkin;
}

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}	
	
}
